# 作者: 宇
# 2025年02月22日20时54分19秒

class Node:
    def __init__(self,elem=-1,lchild=None,rchile=None):
        self.elem=elem
        self.lchild=lchild
        self.rchild=rchile


class Tree:
    def __init__(self):

        self.root=None#树根
        self.queue=[]#辅助队列

    def add_node(self,elem):
        new_node=Node(elem)
        self.queue.append(new_node)
        if self.root is None:
            self.root=new_node
        else:
            if self.queue[0].lchild is None:
                self.queue[0].lchild=new_node
            else:
                self.queue[0].rchild=new_node
                self.queue.pop(0)

    def pre_order(self,current_node):
        if current_node:
            print(current_node.elem,end=' ')
            self.pre_order(current_node.lchild)
            self.pre_order(current_node.rchild)

    def mid_order(self,current_node): # 中序遍历
        if current_node:
            self.min_order(current_node.lchild)
            print(current_node.elem,end=' ')
            self.min_order(current_node.rchild)

    def post_order(self,current_node): # 后序遍历
        if current_node:
            self.post_order(current_node.lchild)
            self.post_order(current_node.rchild)
            print(current_node.elem,end=' ')
    def level_order(self):
        queue=[]
        queue.append(self.root)
        while queue:
            current_node=queue.pop(0)
            print(current_node.elem,end=' ')
            if current_node.lchild:
                queue.append(current_node.lchild)
            if current_node.rchild:
                queue.append(current_node.rchild)

if __name__ == '__main__':
    tree=Tree()#创建树
    for i in range(1,11):#添加10个节点
        tree.add_node(i)#添加节点
    #tree.pre_order(tree.root)#先序遍历
    #tree.post_order(tree.root)后序遍历
    tree.level_order()